<?php
namespace app\modules\company\models;
use common\models\Areas;
use app\models\Basic;

use Yii;

class Company extends Basic
{
    
    public static function tableName()
    {
        return '{{%company}}';
    }

    public function rules()
    {
        return [
            ['company_name', 'required', 'message' => 'company_name不能为空', 'on' => 'add'],
            ['company_name', 'string', 'message' => 'company_name必须是字符串',
                         'min' => 3, 'tooShort' => 'company_name最短3位',
                         'max' => 64, 'tooLong' => 'company_name最长64位'],
            // ['company_profile', 'default', 'value' => ''],
            ['company_profile', 'string', 'message' => 'company_profile必须是字符串'],
            // ['business_license', 'default', 'value' => ''],
            ['business_license', 'string', 'message' => 'business_license必须是字符串', 
                               'max' => 32, 'tooLong' => 'business_license最长32位'],
            // ['country', 'default', 'value' => ''],
            ['country', 'string', 'message' => 'country必须是字符串', 
                        'max' => 16, 'tooLong' => 'country最长16位'],
            // ['province', 'default', 'value' => ''],
            ['province', 'string', 'message' => 'province必须是字符串', 
                         'max' => 16, 'tooLong' => 'province最长16位'],
            // ['city', 'default', 'value' => ''],
            ['city', 'string', 'message' => 'city必须是字符串', 
                     'max' => 16, 'tooLong' => 'city最长16位'],
            // ['district', 'default', 'value' => ''],
            ['district', 'string', 'message' => 'district必须是字符串', 
                         'max' => 16, 'tooLong' => 'district最长16位'],
            // ['address', 'default', 'value' => ''],
            ['address', 'string', 'message' => 'address必须是字符串', 
                        'max' => 128, 'tooLong' => 'address最长128位'],
            // ['zipcode', 'default', 'value' => ''],
            ['zipcode', 'string', 'message' => 'zipcode必须是字符串', 
                        'max' => 16, 'tooLong' => 'zipcode最长16位'],
            ['comid', 'required', 'message' => 'comid不能为空', 'on' => 'add'],
            ['comid', 'string', 'message' => 'comid必须是字符串',
                      'min' => 8, 'tooShort' => 'comid最短8位',
                      'max' => 8, 'tooLong' => 'comid最长8位', 'on' => 'add'],
            // ['contacts', 'default', 'value' => ''],
            ['contacts', 'string', 'message' => 'contacts必须是字符串', 
                         'max' => 64, 'tooLong' => 'contacts最长64位'],
            // ['phones', 'default', 'value' => ''],
            ['phones', 'string', 'message' => 'phones必须是字符串', 
                      'max' => 128, 'tooLong' => 'phones最长128位'],
            // ['emails', 'default', 'value' => ''],
            ['emails', 'string', 'message' => 'emails必须是字符串', 
                      'max' => 256, 'tooLong' => 'emails最长256位'],
            // ['recommend', 'default', 'value' => 0],
            ['recommend', 'integer', 'message' => 'recommend必须为数字，或数字字符串', 
                          'min'=>0, 'tooSmall' => 'recommend最小存储0', 
                          'max'=>4294967295, 'tooBig' => 'recommend最大存储4294967295'],
            // ['level', 'default', 'value' => 1],
            ['level', 'integer', 'message' => 'level必须为数字，或数字字符串'],
            ['level', 'in', 'range' => array(0, 1, 2, 3), 'message' => 'level值必须在0-3中选择'],
            // ['state', 'default', 'value' => 1],
            ['state', 'integer', 'message' => 'state必须为数字，或数字字符串'],
            ['state', 'in', 'range' => array(1, 2, 3, 4), 'message' => 'state值必须在1,2,3,4中选择'],
            ['add_time', 'default', 'value' => '1000-01-01 00:00:00'],
            ['add_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'add_time非yyyy-MM-dd HH:mm:ss格式'],
        ];
    }


    /*添加企业信息*/
    public function addCompany($data)
    {
        $this->scenario = 'add';
        $data['Company']['comid'] = $this->createComid();
        $data['Company']['add_time'] = date('Y-m-d H:i:s', time());
        // P($data);
        if($this->load($data) and $this->validate()){
            if($this->save(false)){
                return $this->comid;
            }
        }
        return false;
    }

    /*
    修改企业信息
    $data   修改的数据
    $id     企业号
    */
    public function modifyCompany($data, $id)
    {
        $company = Company::find()->where('comid = :id', [':id' => $id])->one();
        if(!is_null($company)){
            if($this->load($data) and $this->validate()){
                if($this->userInfo['admin_id'] == 1 and isset($this->company_name)){
                    //必须开发者帐号，才能修改企业名称
                    $company->company_name = $this->company_name;
                }
                if(isset($this->company_profile)){
                    $company->company_profile = $this->company_profile;
                }
                if(isset($this->business_license)){
                    $company->business_license = $this->business_license;
                }
                if(isset($this->country)){
                    $company->country = $this->country;
                }
                if(isset($this->province)){
                    $company->province = $this->province;
                }
                if(isset($this->city)){
                    $company->city = $this->city;
                }
                if(isset($this->district)){
                    $company->district = $this->district;
                }
                if(isset($this->address)){
                    $company->address = $this->address;
                }
                if(isset($this->zipcode)){
                    $company->zipcode = $this->zipcode;
                }
                if(isset($this->contacts)){
                    $company->contacts = $this->contacts;
                }
                if(isset($this->phones)){
                    $company->phones = $this->phones;
                }
                if(isset($this->emails)){
                    $company->emails = $this->emails;
                }
                if(isset($this->state)){
                    $company->state = $this->state;
                }
                // P($company);
                if($company->save(false)){
                    return true;
                }
            }
        };
        return false;
    }


    /*生成企业号*/
    private function createComid()
    {
        // $comid = mt_rand(10, 99) . date('y', time()) . mt_rand(10, 99) . date('m', time());//产生企业号
        $comid = date('d', time()) . date('y', time()) . mt_rand(10, 99) . date('m', time());//产生企业号
        $company = self::find()->where('comid = :id', [':id' => $comid])->one();//查此企业号
        return $company?$this->createComid():$comid;  //如果企业号重复则重新生成
    }

    /*关联查询 省信息*/
    public function getProvince()
    {
        $province = $this->hasOne(Areas::className(), ['id' => 'province'])
                    ->select(['id', 'areaname', 'shortname']);
        return $province;
    }

    /*关联查询 市信息*/
    public function getCity()
    {
        $city = $this->hasOne(Areas::className(), ['id' => 'city'])
                    ->select(['id', 'areaname', 'shortname']);
        return $city;
    }

    /*关联查询 区信息*/
    public function getDistrict()
    {
        $district = $this->hasOne(Areas::className(), ['id' => 'district'])
                    ->select(['id', 'areaname', 'shortname']);
        return $district;
    }

    
}